using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ItemToolTip : MonoBehaviour
{
    public Text itemName;
    public Text itemInfo;

    private RectTransform rectTransform;
    private void Awake()
    {
        rectTransform = GetComponent<RectTransform>();
    }
    private void Update()
    {
        UpdatePosition();
    }

    void OnEnable()
    {
        UpdatePosition();
        // 强制刷新 UI
        LayoutRebuilder.ForceRebuildLayoutImmediate(GetComponent<RectTransform>());
    }

    private void UpdatePosition()
    {
        Vector3 mousePos = Input.mousePosition;

        Vector3[] corners = new Vector3[4];
        // 获取矩形在世界坐标的四个顶点的坐标
        rectTransform.GetWorldCorners(corners);

        float width = corners[3].x - corners[0].x;
        float height = corners[1].y - corners[0].y;

        // UI 坐标系左下角为原点 鼠标靠近屏幕底部时，tooltip 应该显示在鼠标上方（往上偏移半个高度）
        if (mousePos.y < height)
        {
            rectTransform.position = mousePos + Vector3.up * height * 0.6f;
        }
        // 鼠标距离屏幕右侧空余出来的距离大于 tooltip 的宽度
        else if (Screen.width - mousePos.x > width)
        {
            rectTransform.position = mousePos + Vector3.right * width * 0.6f;
        }
        else
        {
            rectTransform.position = mousePos + Vector3.left * width * 0.6f;
        }
    }
    public void SetupToolTop(ItemData_SO itemData)
    {
        itemName.text = itemData.itemName;
        itemInfo.text = itemData.description;
    }

    internal void AddWeaponInfo(AttackData_SO weaponData)
    {
        string info = "\n攻击范围：" + weaponData.attackRange.ToString() + "\n攻击冷却：" + weaponData.coldDown + "\n伤害波动：" + weaponData.minDamage + " - " + weaponData.maxDamage;
        itemInfo.text += info;
    }
}
